package com.springbootblog.service.serviceImpl.fontend;

import com.springbootblog.controller.fontend.IndexController;
import com.springbootblog.dao.ArticleDao;
import com.springbootblog.pojo.IndexArticle;
import com.springbootblog.service.fontend.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FileServiceImpl implements FileService
{
    /**
     * 定义一个静态常量日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    /**
     * 自动装载（dao接口注入）
     */
    @Autowired
    private ArticleDao articleDao;

    @Override
    public Map<String, Object> getFileList()
    {
        // 查文章列表总数
        Integer articleCount = (int)articleDao.selectBySearchAndPageNum(null);
        // 年份列表
        List<String> yearList = articleDao.getPlaceOnFileList();
        Map<String,Object> list = new HashMap<>(yearList.size());
        for (Integer i = 0; i < yearList.size(); i++)
        {
            // 查每一年的文章列表
            List<IndexArticle> indexArticleList = articleDao.getPlaceOnFileListByYear(yearList.get(i));
            Map<String,Object> temp = new HashMap<>(2);
            temp.put("year",yearList.get(i));
            temp.put("list",indexArticleList);
            list.put(i.toString(),temp);
        }
        Map<String,Object> result = new HashMap<>(3);
        result.put("dataList", list);
        result.put("code", 1);
        result.put("articleCount", articleCount);
        return result;
    }
}
